import request from '@/utils/request'
import type { LoginForm, User, UserForm } from '@/types/user'
import type { ApiRes } from '@/types/data'
import type { UserQuery } from '@/types/user'

export const login = (data: LoginForm) => {
  // 告诉axios，服务器响应体(res.data)中的类型就是 { code: number; message: string }
  /**
  return request<{ code: number; data: { token: string }; message: string }>({
    url: 'admin/login',
    method: 'POST',
    data
  })
   */
  return request<ApiRes<{ token: string }>>({
    url: 'admin/login',
    method: 'POST',
    data
  })
}

// 获取用户详情
export const getUserDetail = () => {
  /**
  return request<{ code: number; data: UserDetail; message: string }>({
    url: 'admin/detail'
  })
   */
  return request<ApiRes<User>>({
    url: 'admin/detail'
  })
}

// 获取用户列表
export const getUserList = (params: UserQuery) => {
  return request<ApiRes<{ total: number; items: User[] }>>({
    url: 'admin/list',
    params
  })
}

// 新增用户
export const addUser = (data: UserForm) => {
  return request({
    url: 'admin/add',
    method: 'POST',
    data
  })
}

// 修改用户
export const updateUser = (data: UserForm) => {
  return request({
    url: 'admin/update',
    method: 'POST',
    data
  })
}

// 删除用户
export const delUser = (adminid: string) => {
  return request({
    url: 'admin/delete',
    method: 'POST',
    data: {
      adminid
    }
  })
}
